<?php

namespace app;

use Throwable;
use think\Response;
use think\facade\Db;
use think\facade\Log;
use app\common\util\Result;
use think\exception\Handle;
use UnexpectedValueException;
use app\common\util\EnvHelper;
use Firebase\JWT\ExpiredException;
use think\exception\HttpException;
use think\db\exception\DbException;
use Firebase\JWT\BeforeValidException;
use think\exception\ValidateException;
use app\common\exception\CatchException;
use think\exception\HttpResponseException;
use Firebase\JWT\SignatureInvalidException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

/**
 * 应用异常处理类
 */
class ExceptionHandle extends Handle
{
    /**
     * 不需要记录信息（日志）的异常类列表
     * @var array
     */
    protected $ignoreReport = [
        HttpException::class,
        HttpResponseException::class,
        ModelNotFoundException::class,
        DataNotFoundException::class,
        ValidateException::class,
    ];

    /**
     * 记录异常信息（包括日志或者其它方式记录）
     *
     * @access public
     * @param Throwable $exception
     * @return void
     */
    public function report(Throwable $exception): void
    {
        // 使用内置的方式记录异常日志
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @access public
     * @param \think\Request $request
     * @param Throwable $e
     * @return Response
     */
    public function render($request, Throwable $e): Response
    {
        $devDebug = EnvHelper::getDevDebug();
        $traceId = $GLOBALS['YMS_TRACEID'] ?? '';

        Log::debug("尝试捕获异常[{$e->getCode()}]：" . get_class($e) . " | 提示消息：{$e->getMessage()}" . ($e->getPrevious() ? ' | 父错误：' . get_class($e->getPrevious()) : ''));

        // 应该被捕获的异常
        if ($e instanceof CatchException) {
            // 常规错误
            return Result::restf($e->getCode(), $e->getMessage());
        }

        if ($e instanceof \EasyWeChat\Kernel\Exceptions\HttpException) {
            return Result::restf($e->getCode(), '请求微信api错误' . $e->getMessage());
        }

        // 参数验证错误
        if ($e instanceof ValidateException) {
            return Result::restf(777, "参数验证错误：\n " . $e->getMessage());
        }

        // 只在开放环境打印完整db错误信息
        if (!$devDebug) {
            if ($e instanceof DbException) {
                $traceId = random_int(100000, 999999);
                $now = getNow();
                Log::error("[$traceId] 数据库查询错误：\n " . $e->getTraceAsString());
                Log::error("数据库链接信息：" . json_encode(Db::instance()->getConfig(), JSON_UNESCAPED_UNICODE));//只打印配置，避免超内存
                Log::write("写完错误信息");
                return Result::restf(500, "数据库查询失败 $now");
            }
        }


        if ($e instanceof SignatureInvalidException || $e instanceof UnexpectedValueException) {
            // provided JWT signature verification failed.
            return Result::restf(601, '无效的登录授权，请重新登录');
        } elseif ($e instanceof BeforeValidException) {
            // provided JWT is trying to be used before "nbf" claim OR
            // provided JWT is trying to be used before "iat" claim.
            return Result::restf(602, 'Jwt在授权期之前');
        } elseif ($e instanceof ExpiredException) {
            // provided JWT is trying to be used after "exp" claim.
            return Result::restf(603, 'Jwt授权已过期');
        }

        // 请求异常
        if ($e instanceof HttpException && request()->isAjax()) {
            return response($e->getMessage(), $e->getStatusCode());
        }

        Log::error('未捕获异常：' . $e->getTraceAsString());

        // 其他错误交给系统处理
        return parent::render($request, $e);
    }
}